<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
	var query_keyword = '{{.query_keyword}}';
	var query_dr = '{{.query_dr}}';
	var query_am = '{{.query_am}}';
	
	
	var classid = 0;
	var tables;
	
	if(query_keyword != '') {
			$('#search_key').val(query_keyword);
	}
	$(function(){
		tables = $('#dataTables').DataTable({
			"sAjaxSource" : '{{ urlfor "InventoryController.SelectInventory"}}'
			,"bLengthChange": false
			,"fnServerData" : function(sSource, aoData, fnCallback) {
				var params = checkQueryTableData(aoData)
				$('#search_key').val() ? params.filter = $('#search_key').val() : '';
				if(classid > 0){
					params.ClassId = classid+'';
				}
				params.query_dr = query_dr.replace(/\s*/g,"");
				params.query_am = query_am.replace(/\s*/g,"");
				
				$.post(sSource, JSON.stringify(params),function(resp) {
					fnCallback(resp);
				},'json')
			}
			,columns: [
				{"data" : "Id", "bSortable": false}
				,{render:function(data, type, full, meta) {
					return full.Products.Identifier;
				}, "bSortable": false}
				,{render:function(data, type, full, meta) {
					return full.ProductName;
				}, "bSortable": false}
				
				/*,{render:function(data, type, full, meta) {
					return full.Diopter;
				}, "bSortable": false}
				,{render:function(data, type, full, meta) {
					return full.Astigmatism;
				}, "bSortable": false}
				
				,{render:function(data, type, full, meta) {
					return full.ModelNumber;
				}, "bSortable": false}
				,{render:function(data, type, full, meta) {
					return full.ColourNumber;
				}, "bSortable": false}*/
				
				,{render:function(data, type, full, meta) {
					return full.Products.SuggestedPrice;
				}, "bSortable": false}
				
				
				//,{render:function(data, type, full, meta) {
				//	return full.Products.ClassName;
				//}}
				,{render:function(data, type, full, meta) {
					return full.Products.BrandName;
				}}
				,{"data" : "Inventory",render:function(data, type, full, meta) {
					var cls = 'am-badge';
					if(data<=0) {
						cls =  'am-badge';	
					}else if(full.Products.LowerLimit && data<=full.Products.LowerLimit) {
						cls = 'am-badge am-badge-danger';
					} else  {
						cls = 'am-badge am-badge-primary';
					}
					return '<span class="'+ cls +'">'+ data +'</span>';	
				}}
				
				 ,{   
					"mDataProp": "Action", 
					"bSortable": false, 
					"sWidth":"10%",
					"render": function ( data, type, full, meta ) { 
						btns = '<a href="javascript:;" class="am-btn am-btn-success am-btn-xs" onclick="return selectProducts('+ meta.row +');">选择</a>';
						return btns;  
					}  
				 } 
			]
			,"aLengthMenu":[20, 50, 100, 200]
			,'bLengthChange': true
			,"serverSide": true
			,"bPaginate" : true
			,"bFilter" : false	//是否启动过滤、搜索功能 
			,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
			,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
			  
		});
		
		$("#searchsubmit").bind("submit", function () { //点击按钮 触发table重新请求服务器
			$("#dataTables").dataTable().fnDraw(false);
			return false;
		});
		
		
		$('#dataTables tbody').on('dblclick','tr', function () {
			var i = tables.row(this).index();
			selectProducts(i);
		});
		
	});
	
	function selectProducts(d) {
		var data = tables.row(d).data();
		data.isProduct = 1;
		parent.receiveFormData(data);
		parent.layer.closeAll('iframe');
		return false;
	}
	
	function selectClassToProduct(id) {
		classid = id;
		$("#dataTables").dataTable().fnDraw(false);
	}
	//分类加载
	$.post('{{ urlfor "ErpClassController.DataGrid"}}', function(j) {
		var product_class = $('#product_class');
		if(j.aaData.length > 0) {
			var li = '';
			$.each(j.aaData, function(index, value){
				li += '<li><a href="javascript:;" class="am-text-primary" data-classid="'+ value.Id +'" onclick="selectClassToProduct('+ value.Id +');return false">'+ value.Prefix + value.ClassName +'</a></li>';
			});	
			product_class.html(li);
			pageScrollForPorductClass();
		}
	}, 'json');
	function pageScrollForPorductClass() {
		var windowHeight = $(window).height();
		$('#product_class').slimscroll({
			height: windowHeight - 60,
			color:'#0e90d2',
			alwaysVisible: true,
		});
	}
</script>